package com.goodcol.plugin.actions;

import com.goodcol.plugin.service.CodeGenerateService;
import com.goodcol.plugin.service.TableInfoSettingsService;
import com.goodcol.plugin.tool.CacheDataUtils;
import com.goodcol.plugin.tool.Constants;
import com.goodcol.plugin.tool.MessageDialogUtils;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

/**
 * 表配置菜单
 *
 * @author QuickCode
 * @version 1.0.0
 */
public class ClearTableConfigAction extends AnAction {

    /**
     * 处理方法
     *
     * @param event 事件对象
     */
    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        if (event.getProject() == null) {
            return;
        }
        // 处理选中的表
        if (CodeGenerateService.getInstance(event.getProject()).cacheSelectedTables(event)) {
            DbTable dbTable = CacheDataUtils.getInstance().getSelectDbTable();
            if (dbTable == null) {
                return;
            }
            if (MessageDialogUtils.yesNo("您确定要清除表 " + dbTable.getName() + " 的配置吗？")) {
                TableInfoSettingsService.getInstance().removeTableInfo(dbTable);
                Messages.showInfoMessage(dbTable.getName() + "表配置信息已重置成功", Constants.TITLE_INFO);
            }
        }
    }
}
